# -*- coding = utf-8 -*-
# @Time : 2021/12/17 16:47
# @Author : 龙王赘婿_彪少
# @File : demo06.py
# @Software: PyCharm

# 介绍了绝对布局
# 框布局
# 表格布局

import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QHBoxLayout, \
    QVBoxLayout, QPushButton, QGridLayout, QLineEdit, QTextEdit


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        # self.initUI_01()
        # self.initUI_02()
        # self.initUI_03()
        self.initUI_04()
        self.Show()

    def initUI_01(self):
        # 绝对布局
        # 第二个参数为指定父类
        lab1 = QLabel("标签", self)
        lab1.move(15, 10)
        lab2 = QLabel("图标", self)
        lab2.move(35, 40)
        lab3 = QLabel("菜单", self)
        lab3.move(55, 70)

    def initUI_02(self):
        # 框布局
        btn_ok = QPushButton("ok", self)
        btn_cancel = QPushButton("cancel", self)

        hbox = QHBoxLayout()
        # 加弹簧 参数表示数量
        hbox.addStretch(1)
        hbox.addWidget(btn_ok)
        hbox.addWidget(btn_cancel)

        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        # 设置布局
        self.setLayout(vbox)

    def initUI_03(self):
        # 表格布局
        grid = QGridLayout()
        self.setLayout(grid)
        names = ["Cls", "Bck", "", "Close",
                 "7", "8", "9", "/",
                 "4", "5", "6", "*",
                 "1", "2", "3", "-",
                 "0", ".", "=", "+"]
        # 列表放元组
        positions = [(i, j) for i in range(5) for j in range(4)]
        # 这种for 第一次见
        for position, name in zip(positions, names):

            if name == '':
                continue
            button = QPushButton(name)
            # *(2, 3) 表示拆分元组，拆分完把两个参数写入
            grid.addWidget(button, *position)
            # grid.addWidget(button, position[0], position[1])

    def initUI_04(self):
        title = QLabel("Title")
        author = QLabel("Author")
        review = QLabel("Review")

        titleEdit = QLineEdit()
        authorEdit = QLineEdit()
        reviewEdit = QTextEdit()

        grid = QGridLayout()
        # 设置间距，包括行距与列距
        grid.setSpacing(10)

        grid.addWidget(title, 1, 0)
        grid.addWidget(titleEdit, 1, 1)

        grid.addWidget(author, 2, 0)
        grid.addWidget(authorEdit, 2, 1)

        grid.addWidget(review, 3, 0)
        grid.addWidget(reviewEdit, 3, 1, 5, 1)

        self.setLayout(grid)

    def Show(self):
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle("布局")
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
